package io.sdl.common.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.sdl.common.service.impl.CrudServiceImpl;
import io.sdl.common.sys.dto.SdlInstitutionDTO;
import io.sdl.common.sys.vo.SdlInstitutionVo;
import io.sdl.common.utils.ConvertUtils;
import io.sdl.common.sys.dao.SdlInstitutionDao;
import io.sdl.common.sys.entity.SdlInstitutionEntity;
import io.sdl.common.sys.service.SdlInstitutionService;
import cn.hutool.core.util.StrUtil;
import io.sdl.common.utils.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@Service
public class SdlInstitutionServiceImpl extends CrudServiceImpl<SdlInstitutionDao, SdlInstitutionEntity, SdlInstitutionDTO> implements SdlInstitutionService {

    @Autowired
    private SdlInstitutionDao sdlInstitutionDao;

    @Override
    public QueryWrapper<SdlInstitutionEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SdlInstitutionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<SdlInstitutionEntity> getList(Map<String, Object> params) {
        return baseDao.getList(params);
    }

    @Override
    public SdlInstitutionDTO getbyId(Long id) {
        SdlInstitutionEntity entity = baseDao.getById(id);

        return ConvertUtils.sourceToTarget(entity, SdlInstitutionDTO.class);
    }

    @Override
    public int updateFromId(SdlInstitutionDTO institution) {
        SdlInstitutionEntity entity = ConvertUtils.sourceToTarget(institution, SdlInstitutionEntity.class);
        return sdlInstitutionDao.updateFromId(entity);
    }

    @Override
    public int addOrganization(SdlInstitutionDTO institution) {
        SdlInstitutionEntity entity = ConvertUtils.sourceToTarget(institution, SdlInstitutionEntity.class);
//         insert(entity);
        //保存
         return sdlInstitutionDao.insert(entity);
    }

    @Override
    public List<SdlInstitutionVo> findAll(Map<String, Object> params) {

        return baseDao.findAll(params);
    }

    @Override
    public Integer saveUploadedFile(MultipartFile file, Long id,String identification) {
        try {

            String accessUrl = "/uploads/" + UploadFileUtils.uploadFile(file);

            SdlInstitutionEntity sdlInstitutionEntity = baseDao.selectById(id);
            if(identification.equals("identification")){
                sdlInstitutionEntity.setImageUrls(accessUrl);
            }
            if(identification.equals("logo")){
                sdlInstitutionEntity.setInstitutionLogo(accessUrl);
            }
            return baseDao.updateById(sdlInstitutionEntity);
        } catch (IOException e) {
            throw new RuntimeException("文件存储失败", e);
        }
    }

    @Override
    public SdlInstitutionEntity numberCheck(String businessLicenseNumber) {
        return baseDao.numberCheck(businessLicenseNumber);
    }
}